package Ep02_LinkedList._0142;

import Ep02_LinkedList.ListNode;

/**
 * @author Jimmy Zhan WORKSTATION
 * @date 2023/3/10 11:32
 * 说明： 力扣 0142 环形链表
 *
 *
 */

public class _0142 {
    public ListNode detectCycle(ListNode head) {
        if (head == null || head.next == null) return null;
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null){
            slow = slow.next;
            fast = fast.next.next;
            if (fast == slow){
                ListNode meet1 = fast;
                ListNode meet2 = head;
                while (meet1 != meet2){
                    meet1 = meet1.next;
                    meet2 = meet2.next;
                }
                return meet1;
            }
        }
        return null;
    }
}
